package com.leyou.utils;


import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.List;

public class ResponseEntityUtils {


    /**
     * 对数据库进行  查询  时,查询到数据返回200, 查询结果为空返回204
     * @param t
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<T> selectResultStatus(T t) {
        try {
            if (t != null) {
                return ResponseEntity.ok(t);
            }
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            System.out.println("\n工具类ResponseEntityUtils:对象方法异常\n");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }


    /**
     * 对数据库进行  查询  时,查询到数据返回200, 查询结果为空自定义返回状态码
     * @param t
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<T> selectResultStatus(T t,HttpStatus httpStatus) {
        try {
            if (t != null) {
                return ResponseEntity.ok(t);
            }
            return ResponseEntity.status(httpStatus).build();
        } catch (Exception e) {
            System.out.println("\n工具类ResponseEntityUtils:对象方法异常\n");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 对数据库进行  查询  时,查询到数据返回200, 查询结果为空返回204
     * @param list
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<List<T>> selectResultStatus(List<T> list) {
        try {
            if (list != null && 0 != list.size()) {
                return ResponseEntity.ok(list);
            }
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            System.out.println("\n工具类ResponseEntityUtils:对象集合异常\n");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }


    /**
     * 对数据库进行  查询  时,查询到数据返回200, 查询结果为空可自定义返回类型
     * @param list
     * @param httpStatus 用户自定义的返回类型
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<List<T>> selectResultStatus(List<T> list,HttpStatus httpStatus) {
        try {
            if (list != null && 0 != list.size()) {
                return ResponseEntity.ok(list);
            }
            return ResponseEntity.status(httpStatus).build();
        } catch (Exception e) {
            System.out.println("\n工具类ResponseEntityUtils:对象集合异常\n");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
}
